/**
 * Created by clown on 2016/1/17.
 */


(function($){

	var $pagination = $('<div></div>');
	$pagination.addClass("m-pagination").attr("id","page");
	$(".person-content").append($("#page"));
	$("#page").pagination({
		pageSize: 10,
		total: 1000,
		pageBtnCount: 6,
		showFirstLastBtn:false,
		prevBtnText:"<",
		nextBtnText:">"
	});
	$("#page").on("pageClicked", function (event, data) {
		render(data);
	});

	var projectHtml = $("#project-template").html();
	var projectTpl= Handlebars.compile(projectHtml);
	var render = function(opts) {
		//remember data
		if(!!window.sessionStorage.getItem("projectJson")){
			var projectJson = window.sessionStorage.getItem("projectJson");
		}
		$.get("/lt/project/list", _.extend({}, opts, projectJson),function(data){
			if(!data.result){
				var projectData = data.data;
				$(".project-list-show").html(projectTpl(data));
				$("#page").pagination('setPageSize', data.total);

				for(var i=0; i < projectData.length; i++) {
					//环形进度条调用
					var cricleStyleOne = {
						backgroundColor: "#fff",//background colour of inner circle
						fontColor: "#ecb22e",//font color of progress text
						fontSize: "20px",//font size of progress text
						radius: 40,//radius of circle
						progressBarBackground: "#cdcdcd",//background colour of circular progress Bar
						progressBarColor: "#ecb22e",//colour of circular progress bar
						progressBarWidth: 10,//progress bar width
						progressPercent: 0,//progress bar width
						showText: true //show progress text or not
					};
					var cricleStyle = {
						backgroundColor: "#fff",//background colour of inner circle
						fontColor: "#ecb22e",//font color of progress text
						fontSize: "20px",//font size of progress text
						radius: 40,//radius of circle
						progressBarBackground: "#cdcdcd",//background colour of circular progress Bar
						progressBarColor: "#ecb22e",//colour of circular progress bar
						progressBarWidth: 10,//progress bar width
						progressPercent: projectData[i].finace_rate,//progress percentage out of 100
						progressValue: projectData[i].finace_rate,//diplay this value instead of percentage
						showText: true//show progress text or not
					};
					if (projectData[i].finace_rate > 50) {
						cricleStyleOne.fontColor = "#66b56a";
						cricleStyleOne.progressBarColor = "#66b56a";
						cricleStyle.fontColor = "#66b56a";
						cricleStyle.progressBarColor = "#66b56a";
					}
					//第一次随便写，如果只写一遍则不会显示%
					$("#circlePro" + i).circularloader(cricleStyleOne);
					//第二次调用显示%
					$("#circlePro" + i).circularloader(cricleStyle);
				}
			}
		});
	};
	render();

	var industryHtml = $("#industry-template").html();
	var industryTpl= Handlebars.compile(industryHtml);
	var industryList = function(){
		$.get("/lt/industry/list",function(data){
			if(!data.result){
				$(".project-investment-industry").html(industryTpl(data));
			}
		});
	};
	industryList();

	var cityHtml = $("#city-template").html();
	var cityTpl= Handlebars.compile(cityHtml);
	var cityList = function(){
		$.get("/lt/project/citylist",function(data){
			if(!data.result){
				$(".project-each-district").html(cityTpl(data));
			}
		});
	};
	cityList();


	//搜索
	$(".project-submit").click(function(){
		var projectJson = {
			key: $(".project-search-content input").val(),
			status: $(".project-status > .project-selected").attr("data-status"),
			investFieldId: $(".project-investment-industry > .project-selected").attr("data-id"),
			cityID: $(".project-each-district > .project-selected").attr("data-id"),
			pageIndex: 1,
			pageSize: 20
		};
		window.sessionStorage.setItem("projectJson", projectJson);
		render(projectJson);
	});
	
	//详情页
	$(".project_detail").click(function() {
		var projId = $(".project-detail-show").attr("data-id");
		alert(projId);
		
	});
	
})(jQuery);